package com.itmgg.web.utils;

import com.itmgg.web.websocket.SocketIns;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.util.CollectionUtils;
import org.springframework.util.MultiValueMap;
import org.yeauty.pojo.Session;

import java.util.List;
import java.util.UUID;

/**
 * Created by rui.song on 2021/06/26.
 */
public class WebSocketUtils {
    public static SocketIns buildSession(MultiValueMap reqMap, Session session) {
        List<String> instanceIds = (List<String>)reqMap.get("instanceId");
        List<String> deploymentIds = (List<String>) reqMap.get("deploymentId");
        List<String> resourceNames = (List<String>) reqMap.get("resourceName");
        SocketIns socketIns = new SocketIns().
                withInstanceId(CollectionUtils.isEmpty(instanceIds)? "" : instanceIds.get(0)).
                withSocketId(UUID.randomUUID().toString()).
                withDeploymentId(CollectionUtils.isEmpty(deploymentIds)? "" : deploymentIds.get(0)).
                withResourceName(CollectionUtils.isEmpty(resourceNames)? "" : resourceNames.get(0));
        session.setAttribute("socketIns", socketIns);
        return socketIns;
    }

    public static String calculateStatus(ProcessInstance internalProcessInstance) {
        if (internalProcessInstance.isSuspended()) {
            return ProcessInstanceStatus.SUSPENDED.name();
        } else {
            return internalProcessInstance.isEnded() ? ProcessInstanceStatus.COMPLETED.name() : ProcessInstanceStatus.RUNNING.name();
        }
    }
}
